
<template>
  <div class="container-change">
    <el-card>
      <div class="wall-one">
        <span>说明：目前支持学科和关键字条件筛选</span>
        <el-button
          icon="el-icon-edit"
          @click="$router.push('new')"
          type="success"
          size="small"
          >新增实体</el-button
        >
      </div>
      <div class="box">
        <el-form label-width="80px" size="small">
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select
                  @change="changeSubject"
                  v-model="requestParams.subjectID"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in subject"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select
                  v-model="requestParams.catalogID"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in dir"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select
                  v-model="requestParams.tags"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in tag"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input
                  v-model="requestParams.keyword"
                  placeholder="根据内容搜索"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select
                  v-model="requestParams.questionType"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in question"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select
                  v-model="requestParams.difficulty"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in difficult"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select
                  v-model="requestParams.direction"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in overGrounds"
                    :key="item"
                    :label="item"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select
                  v-model="requestParams.user"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in userOptions"
                    :key="item.id"
                    :label="item.username"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="实体备注">
                <el-input
                  v-model="input"
                  placeholder="请输入内容"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业备注">
                <el-input
                  v-model="input"
                  placeholder="请输入内容"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-select
                  @change="handCity"
                  v-model="requestParams.povenCe"
                  placeholder="请选择"
                  style="width: 48%; margin-right: 2%"
                >
                  <el-option
                    v-for="item in Province"
                    :key="item"
                    :label="item"
                    :value="item"
                  >
                  </el-option>
                </el-select>
                <el-select
                  v-model="requestParams.city"
                  placeholder="请选择"
                  style="width: 50%"
                >
                  <el-option
                    v-for="item in DezhouCity"
                    :key="item"
                    :label="item"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item style="text-align: right">
                <el-button @click="clear()">清楚</el-button>
                <el-button type="primary" @click="filter()">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="content">
        <el-tabs
          v-model="requestParams.chkState"
          type="card"
          @tab-click="changeTab"
        >
          <el-alert
            :title="`数据一共 ${total} 条`"
            style="margin-bottom: 15px"
            type="info"
            class="alert"
            :closable="false"
            show-icon
          >
          </el-alert>
          <el-tab-pane label="全部" name="all"></el-tab-pane>
          <el-tab-pane label="待审核" name="0"></el-tab-pane>
          <el-tab-pane label="已审核" name="1"></el-tab-pane>
          <el-tab-pane label="已拒绝" name="2"></el-tab-pane>
        </el-tabs>
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column fixed prop="number" label="日期" width="150">
          </el-table-column>
          <el-table-column prop="subject" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="catalog" label="省份" width="120">
          </el-table-column>
          <el-table-column label="题型" width="120px">
            <template slot-scope="scope">
              {{
                questionType.find(
                  (item) => item.value === +scope.row.questionType
                ).label
              }}
            </template>
          </el-table-column>
          <el-table-column prop="address" label="地址" width="300">
            <template slot-scope="scope">
              <div v-html="scope.row.question"></div>
            </template>
          </el-table-column>
          <el-table-column label="录入时间" width="180">
            <template slot-scope="scope">
              {{ scope.row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column label="难度" width="180"
            ><template slot-scope="scope">
              {{
                difficulty.find((item) => item.value === +scope.row.difficulty)
                  .label
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="录入人"
            width="180"
            prop="creator"
          ></el-table-column>
          <el-table-column label="审核状态" width="180">
            <template slot-scope="scope">
              {{ scope.row.chkState | state_chk }}
            </template>
          </el-table-column>
          <el-table-column
            label="审核意见"
            width="180"
            prop="chkRemarks"
          ></el-table-column>
          <el-table-column
            label="审核人"
            width="180"
            prop="chkUser"
          ></el-table-column>
          <el-table-column label="发布状态" width="180"
            ><template slot-scope="scope">
              {{ scope.row | state_pub }}
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="200px"
          >
            <template slot-scope="scope">
              <el-button
                @click="openPreviewDialog(scope.row)"
                type="text"
                class="xiao"
                >预览</el-button
              >
              <el-button
                type="text"
                class="xiao"
                @click="openCheckDialog(scope.row)"
                >审核</el-button
              >
              <el-button
                type="text"
                class="xiao"
                :disabled="scope.row.publishState === 1"
                @click="$router.push(`new`)"
                >修改</el-button
              >
              <el-button
                type="text"
                class="xiao"
                @click="togglePublish(scope.row)"
                >{{ scope.row.publishState === 1 ? '下架' : '上架' }}</el-button
              >
              <el-button
                type="text"
                class="xiao"
                :disabled="scope.row.publishState === 1"
                @click="DeleteFW(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        @current-change="pager"
        :page-sizes="[5, 10, 20, 50]"
        @size-change="handleSizeChange"
      ></el-pagination>
      <questions-preview
        :data="questionInfo"
        ref="questionPreview"
      ></questions-preview>
      <questions-check
        :data="questionInfo"
        :dialogVisible="checkDialogVisible"
        @close="checkDialogVisible = false"
        @updateList="getList()"
      ></questions-check>
    </el-card>
  </div>
</template>

<script>
import {
  choice as questionList,
  remove as questionDelte,
  choicePublish,
} from '@/api/hmmm/questions'
import { simple as directoryList } from '@/api/hmmm/directorys'
import { simple as tagList } from '@/api/hmmm/tags'
import { simple as subjectList } from '@/api/hmmm/subjects'
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
import { simple as userList } from '@/api/base/users.js'
import { provinces as getCity, citys as getArea } from '@/api/hmmm/citys'
import QuestionsPreview from '../components/questions-preview.vue'
import QuestionsCheck from '../components/questions-check.vue'

export default {
  filters: {
    state_chk(lin) {
      if (lin === 0) return '未审核'
      if (lin === 1) return '已审核'
      if (lin === 2) return '已拒接'
    },
    state_pub(row) {
      if (row.publishState === 1 && row.chkState === 1) {
        return '已发布'
      }
      if (row.publishState === 0 && row.chkState === 1) {
        return '已下架'
      }
      return '待发布'
    },
  },
  name: 'xin',
  props: {},

  data() {
    return {
      dialogVisible: false,
      questionInfo: {},
      difficulty,
      direction,
      questionType,
      requestParams: {
        subjectID: null,
        catalogID: null,
        tags: null,
        povenCe: null,
        city: null,
        user: null,
        difficulty: null,
        direction: null,
        chkState: null,
        questionType: null,
        keyword: null,
        chkState: 'all',
        pagesize: 5,
        page: 1,
      },

      dir: [],
      subject: [],
      tag: [],
      tableData: [],
      userOptions: [],
      question: questionType,
      difficult: difficulty,
      overGrounds: direction,
      Province: getCity(),
      DezhouCity: [],
    }
  },

  async created() {
    const { data: subjectArr } = await subjectList()

    this.subject = subjectArr
    const { data: userArr } = await userList()

    this.userOptions = userArr

    this.getList()
  },

  methods: {
    async togglePublish(question) {
      await this.$confirm(
        `您确认${question.publishState === 1 ? '下架' : '上架'}这道题目吗?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
      question.publishState = question.publishState === 1 ? 0 : 1
      await choicePublish(question)
      this.$message.success(
        `${question.publishState === 1 ? '上架' : '下架'}成功`
      )
      this.getList()
    },
    DeleteFW(question) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          await questionDelte(question)
          this.$message.success('删除成功')
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    clear() {
      for (const key in this.requestParams) {
        if (key !== 'page' && key !== 'pagesize') this.requestParams[key] = null
      }
      this.getList()
    },
    changeTab() {
      this.requestParams.page = 1
      this.getList()
    },
    filter() {
      this.requestParams.page = 1
      this.getList()
    },
    async changeSubject(subjectID) {
      this.requestParams.catalogID = null
      this.requestParams.tags = null
      if (subjectID) {
        const { data: re } = await directoryList({ subjectID })
        this.dir = re
        const { data: tagArr } = await tagList({ subjectID })
        this.tag = tagArr
      } else {
        this.dir = []
        this.tag = []
      }
    },
    handCity(cityName) {
      this.requestParams.city = null
      this.DezhouCity = getArea(cityName)
    },
    openPreviewDialog(questionInfo) {
      this.questionInfo = questionInfo
      this.$nextTick(() => {
        this.$refs.questionPreview.open()
      })
    },
    openCheckDialog(questionInfo) {
      this.questionInfo = questionInfo
      this.checkDialogVisible = true
    },
    async getList() {
      const params = { ...this.requestParams }
      if (params.chkState === 'all') params.chkState = null
      const { data: questionsData } = await questionList(params)
      this.tableData = questionsData.items
      this.total = questionsData.counts
    },
  },
  computed: {},
  watch: {},
  mounted() {},
  components: {
    QuestionsPreview,
    QuestionsCheck,
  },
}
</script>

<style scoped lang='less'>
.container-change {
  .wall-one {
    font-size: 12px;
    color: deeppink;
    margin-bottom: 35px;
    justify-content: space-between;
    display: flex;
  }
  .xiao {
    font-size: 12px;
  }
}
</style>
